/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include "subcmd.h"

/**
 * @brief       qp job mod handler, to modify a job in the cluster.
 *
 * @param[in]   argc          #1: number of input argument
 * @param[in]   argv          #2: string array of the input argument
 *
 * @retval      0             #1: succeed
 * @retval      1             #2: input option is wrong or kind of input argument not specified
 * @retval      2             #3: argument has syntax error
 * @retval      3             #4: failed to modify the job
 * @retval      4             #5: not enough memory
 */
int
cmd_modify_job(int argc, char **argv)
{
    struct request req;
    if (set_submit_request(argc, argv, CMD_MODIFY_JOB, &req) < 0) {
        fprintf(stderr, ". Job not modified.\n");
        exit(1);
    }

    char *job=NULL;
    time_t beginTime=0, termTime=0;
    unsigned int i;
    for (i=0; i<req.number; i++) {
        switch (req.keyvalues[i].key) {
	case REQUEST_KEY_MODIFY_JOBID:
            job = req.keyvalues[i].value;
            break;
        case REQUEST_KEY_SUBMIT_BEGINTIME:
            beginTime = atol(req.keyvalues[i].value);
            break;
        case REQUEST_KEY_SUBMIT_TERMTIME:
            termTime = atol(req.keyvalues[i].value);
            break;
        }
    }

    JOBID_T jobId = -1, *jobIdList = NULL;
    int numJobIds;
    numJobIds = get_jobids_fromstring(job, &jobIdList);
    if (0 > numJobIds) {
        exit(2);
    }

    jobId = jobIdList[0]; // modify one job at one time.

    struct QuickPoolSubmitReply reply;
    memset(&reply, 0, sizeof(struct QuickPoolSubmitReply));

    int ret;
    ret = client_modify_job(&req, &reply, jobId, NULL);
    if (0 > ret) {

        if (strlen(reply.error_message) > 0) { // JS reply error message
            fprintf(stderr, "%s\n", reply.error_message);
        } else {
            fprintf(stderr, "Job not modified due to above error.\n");
        }

        gf_xdr_free(libxdr_submit_reply, &reply);
        gf_xdr_free(gf_xdr_request, &req);

        exit(3);
    }

    char strJobId[32];
    printf("Parameters of job <%s> are being changed\n", jobid_2string(jobId, strJobId));
    char timebuf[TIMESTRINGLEN];
    if (0 < beginTime) {
        fprintf(stderr, "Job will be scheduled after %s\n",
                ctime_2string(CTIME_FORMAT_a_b_d_T_Y, &beginTime, timebuf));
    }

    if (0 < termTime) {
        fprintf(stderr, "Job will be scheduled after %s\n",
                ctime_2string(CTIME_FORMAT_a_b_d_T_Y, &termTime, timebuf));
    }

    gf_xdr_free(libxdr_submit_reply, &reply);
    gf_xdr_free(gf_xdr_request, &req);

    exit(0);
} // end function cmd_modify_job
